package com.future.hedongjun.mine.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.ToastUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.Contant;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.activity.ConfirmOrderActivity;
import com.future.hedongjun.mine.adapter.DeliveryAddressAdapter;
import com.future.hedongjun.mine.bean.DeliveryAddressBean;
import com.future.hedongjun.mine.bean.response.AddressResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * description: 收货地址
 * autour: gxcheng
 * date: 2017/9/13 23:59
 */
public class DeliveryAddressActivity extends BaseActivity {

    @BindView(R.id.delivery_address_rv)
    RecyclerView mRvDeliveryAddress;
    private DeliveryAddressAdapter mAdapter;
    private List<DeliveryAddressBean> mAddressList;
    private String type;
    private View emptyView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivery_address);
        ButterKnife.bind(this);
        setTitle("收货地址");

        emptyView = findViewById(R.id.no_address_llt);
        mAddressList = new ArrayList<>();
        mRvDeliveryAddress.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DeliveryAddressAdapter(R.layout.item_delivery_address, mAddressList);
        mRvDeliveryAddress.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.isEmpty(type)) {
//                    Intent intent = new Intent(mContext, DeliveryAddressEditActivity.class);
//                    intent.putExtra(Contant.KEY_ADDRESS_ID, mAdapter.getData().get(position).getId());
//                    startActivityForResult(intent, Contant.const_10010);
                } else {
                    Intent data = new Intent();
                    DeliveryAddressBean bean = mAdapter.getData().get(position);
                    data.putExtra("address", bean);
                    setResult(ConfirmOrderActivity.CHOOSE_ADDRESS, data);
                    finish();
                }
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, DeliveryAddressEditActivity.class);
                intent.putExtra(Contant.KEY_ADDRESS_ID, mAdapter.getData().get(position).getAddress_id());
                intent.putExtra("address",mAdapter.getData().get(position));
                startActivityForResult(intent, Contant.const_10010);
            }
        });

        findViewById(R.id.add_delivery_address_btn_tv1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(DeliveryAddressEditActivity.class, Contant.const_10010);
            }
        });

        initDatas();
    }

    private void initDatas() {
        new HttpRequest<AddressResponse>()
                .with(this)
                .setPath("/addons/litestore/api.adress/lists")
                .addParam("token",MyApp.getInstance().getUserInfo().getToken())
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        List<DeliveryAddressBean> data = ((AddressResponse) response).data;
                        if (data == null || data.size() ==0) {
                            emptyView.setVisibility(View.VISIBLE);
                        }else{
                            emptyView.setVisibility(View.GONE);
                        }
                        mAdapter.setNewData(data);
                    }
                }).start(new AddressResponse());
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        type = extras.getString("type", "");
    }

    /*跳转到编辑地址页面*/
    @OnClick(R.id.add_delivery_address_btn_tv)
    public void toEditAddress() {
        startActivityForResult(DeliveryAddressEditActivity.class, Contant.const_10010);
    }

    private Handler handler = new Handler();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Contant.const_10010 && resultCode == RESULT_OK) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    initDatas();
                }
            },320);

        }
    }
}
